package com.bite.demo.controller;

import com.bite.demo.entity.AlarmMessage;
import com.bite.demo.mail.Mail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/alarm")
@RestController
public class AlarmController {
    @Autowired
    private Mail mail;
    @RequestMapping("/handler")
    public String handler(@RequestBody List<AlarmMessage> alarmMessages){
        log.info("收到告警信息:{}", alarmMessages);
        mail.sendMail("2046767906@qq.com", buildContent(alarmMessages));
        return "接收报警信息";
    }

    private String buildContent(List<AlarmMessage> alarmMessages) {
        StringBuilder content = new StringBuilder();
        for(AlarmMessage message:alarmMessages){
            content.append("<br/>scopeId:").append(message.getScopeId())
                    .append("<br/>scope:").append(message.getScope())
                    .append("<br/>name:").append(message.getName())
                    .append("<br/>id0:").append(message.getId0())
                    .append("<br/>ruleName:").append(message.getRuleName())
                    .append("<br/>alarmMessage:").append(message.getAlarmMessage())
                    .append("<br/>startTime:").append(message.getStartTime())
                    .append("<br/>-----------");
        }
        return content.toString();
    }

}
